import os

from docx import Document
from docx.shared import Inches, Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml import OxmlElement
from docx.oxml.ns import qn



class WordReport():
    # 图片的标题
    ###文档对象 图片路径 图片序号 图片题注
    def picTitle(self,doc, pic, index, name):
        # 生成图片
        paragraph = doc.add_paragraph()
        run = paragraph.add_run()
        run.add_picture(pic, width=Inches(5))
        paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
        # 生成题注与标签
        paragraph = doc.add_paragraph()
        run = paragraph.add_run()
        paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
        r = run._r
        start = OxmlElement('w:bookmarkStart')
        start.set(qn('w:id'), str(index))
        start.set(qn('w:name'), str(index))
        run.element.append(start)

        text = OxmlElement('w:r')
        text.text = "图"
        text.set(qn('xml:space'), 'preserve')
        r.append(text)
        ####
        # run2 = paragraph.add_run()
        r2 = OxmlElement('w:r')
        fldChar = OxmlElement('w:fldChar')
        fldChar.set(qn('w:fldCharType'), 'begin')
        r2.append(fldChar)
        r.append(r2)

        r2 = OxmlElement('w:r')
        instrText = OxmlElement('w:instrText')
        instrText.text = ' SEQ 图 \* ARABIC '
        instrText.set(qn('xml:space'), 'preserve')
        r2.append(instrText)
        r.append(r2)

        r2 = OxmlElement('w:r')
        fldChar = OxmlElement('w:fldChar')
        fldChar.set(qn('w:fldCharType'), 'separate')
        r2.append(fldChar)
        r.append(r2)

        r2 = OxmlElement('w:r')
        t = OxmlElement('w:t')
        t.text = str(index)
        r2.append(t)
        r.append(r2)

        r2 = OxmlElement('w:r')
        fldChar = OxmlElement('w:fldChar')
        fldChar.set(qn('w:fldCharType'), 'end')
        r2.append(fldChar)
        r.append(r2)
        ####

        end = OxmlElement('w:bookmarkEnd')
        end.set(qn('w:id'), str(index))
        r.append(end)

        paragraph.add_run(" " + name)

        # run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
        # Doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')

    # 交叉引用
    def section(self,doc, index, _text):
        paragraph = doc.add_paragraph(_text + "如")
        run = paragraph.add_run()
        r = run._r
        r2 = OxmlElement('w:r')
        fldChar = OxmlElement('w:fldChar')
        fldChar.set(qn('w:fldCharType'), 'begin')
        r2.append(fldChar)
        r.append(r2)

        r2 = OxmlElement('w:r')
        instrText = OxmlElement('w:instrText')
        instrText.text = ' REF ' + str(index) + ' \h '
        r2.append(instrText)
        r.append(r2)

        r2 = OxmlElement('w:r')
        fldChar = OxmlElement('w:fldChar')
        fldChar.set(qn('w:fldCharType'), 'separate')
        r2.append(fldChar)
        r.append(r2)

        r2 = OxmlElement('w:r')
        t = OxmlElement('w:t')
        t.text = '图' + str(index)
        r2.append(t)
        r.append(r2)

        r2 = OxmlElement('w:r')
        fldChar = OxmlElement('w:fldChar')
        fldChar.set(qn('w:fldCharType'), 'end')
        r2.append(fldChar)
        r.append(r2)

        paragraph.add_run("所示。")
    def addItem(self,path):
        path = path.replace('/', '\\')
        print(os.getcwd(),path)
        Doc = Document()
        Doc.styles['Normal'].font.name = u'宋体'
        Doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
        imgRowList=[]
        imgNameList=[]
        for root, dirs, files in os.walk(path):
            for file in files:
                if os.path.splitext(file)[1] == '.png' or os.path.splitext(file)[1] == '.jpg':
                    imgRowList.append(os.path.join(root, file))
                    imgNameList.append(os.path.splitext(file)[0])
        for i in range(0, len(imgNameList)):
            self.section(Doc, i+1, imgNameList[i])
        for i in range(0, len(imgRowList)):
            self.picTitle(Doc,imgRowList[i],i+1,imgNameList[i])
        paths=path+"\报告.docx"
        Doc.save(paths)
        return paths

# if __name__ == '__main__':
#     addItem(os.getcwd())
    # Doc.save("test.docx")

    # Doc = Document()
    # Doc.styles['Normal'].font.name = u'宋体'
    # Doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
    #
    # section(Doc, 1,"test")
    # section(Doc, 2, "test2")
    # picTitle(Doc,"test.jpg",1,"test")
    # picTitle(Doc, "test.jpg", 2, "test2")
    #
    # # pa =  Doc.add_paragraph("交叉引用")  # 在这里paragraph指的就是一个段落的意思
    #
    # Doc.save("test.docx")